# Copyright (c) 2007-2009 Google Inc.
# Copyright (c) 2006-2007 Jaiku Ltd.
# Copyright (c) 2002-2006 Mika Raento and Renaud Petit
#
# This software is licensed at your choice under either 1 or 2 below.
#
# 1. Perl (Artistic) License
#
# This library is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
# 2. Gnu General Public license 2.0
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
#
# This file is part of the JaikuEngine mobile frontend.

# Author: mikie@google.com (Mika Raento)

use strict;
use Test::More tests => 3;

use Danga::Socket;
use Jaiku::Http::DNSLookup;
use Jaiku::Http::DNSResolver;
use Net::DNS::Packet;

use lib 't/lib';
use DNSTest;

my @reply;
my @error;

use constant HOST_NAME => 'simple1.example.org';
use constant HOST_IP   => '10.0.0.1';

my @methods = (
  AnswerWithA->new(HOST_NAME, HOST_IP)
);

Danga::Socket->SetLoopTimeout(10);
Danga::Socket->SetPostLoopCallback(sub {
  return ($#reply < 2);
});

my $dns_cb = sub {
  my ($reply, $error) = @_;
  push(@reply, $reply);
  push(@error, $error);
};

my $resolver = Jaiku::Http::DNSResolver->get();

DNSTest::run_with_server(\@methods, sub {
    my ($server_port) = @_;
    Jaiku::Http::DNSLookup->set_server('127.0.0.1', $server_port);
    $resolver->resolve_a(HOST_NAME(), $dns_cb, timeout => 1.0);
    $resolver->resolve_a(HOST_NAME(), $dns_cb, timeout => 1.0);
    $resolver->resolve_a(HOST_NAME(), $dns_cb, timeout => 1.0);
    Danga::Socket->EventLoop();
});

ok($reply[0], "got reply " . ($reply[0] && $reply[0]->address()));
is($error[0], undef);
is(Jaiku::Http::DNSLookup->number_of_lookups(), 1, "merged lookups");
